<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>物资基本信息</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称"
                v-model="name" suffix-icon="el-icon-search"></el-input>
      <el-input style="width: 200px" placeholder="请输入类型"
                v-model="username" suffix-icon="el-icon-user" class="ml-5 mr-5"></el-input>
      <el-button class="ml-5" type="primary" @click="search">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd" class="mr-5">新增物资 <i class="el-icon-circle-plus-outline"></i>
      </el-button>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='再想想'
          icon="el-icon-info"
          icon-color="red"
          title="确定批量删除吗？"
          @confirm="delBatch">
        <el-button type="danger" slot="reference" class="mr-5">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>
    <el-table :data="tableData" border stripe
              @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="40">
      </el-table-column>
      <el-table-column prop="id" label="id" width="40">
      </el-table-column>
      <el-table-column prop="type" label="类型" width="100">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="100">
      </el-table-column>
      <el-table-column prop="barCode" label="条码编号" width="100">
      </el-table-column>
      <el-table-column prop="materialSpace" label="型号规格" width="100">
      </el-table-column>
      <el-table-column prop="units" label="单位" width="50">
      </el-table-column>
      <el-table-column prop="typeName" label="适用车型" width="100">
      </el-table-column>
      <el-table-column prop="packingType" label="包装类型" width="100">
      </el-table-column>
      <el-table-column prop="pileUpSpace" label="堆放占用空间" width="100">
      </el-table-column>
      <el-table-column prop="stock" label="库存数量" width="100">
      </el-table-column>
      <el-table-column prop="materialprice.inPrice" label="进价" width="100">
      </el-table-column>
      <el-table-column prop="materialprice.outPrice" label="售价" width="100">
      </el-table-column>
      <el-table-column prop="materialprice.supplier" label="供应商" width="100">
      </el-table-column>
      <el-table-column label="操作" width="" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)" class="mr-5">
            查看<i class="el-icon-edit"></i>
          </el-button>
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='再想想'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="del(scope.row.id)">
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size=pageSize
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>
    <!--    物资信息的弹窗    -->
    <el-dialog title="详细信息" :visible.sync="dialogFormVisible" width="50%" :close-on-click-modal="false">
      <el-form :model="material" :inline="true" :rules="rules" ref="material">
        <el-form-item label="id" label-width="120px" prop="id">
          <el-input v-model="material.id" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="类型" label-width="120px" prop="type">
          <el-input v-model="material.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" label-width="120px" prop="name">
          <el-input v-model="material.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="条码编号" label-width="120px" prop="barCode">
          <el-input v-model="material.barCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="型号规格" label-width="120px" prop="materialSpace">
          <el-input v-model="material.materialSpace" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位" label-width="120px" prop="units">
          <el-input v-model="material.units" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="适用车型" label-width="120px" prop="typeName">
          <el-input v-model="material.typeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="包装类型" label-width="120px" prop="packingType">
          <el-input v-model="material.packingType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="堆放占用空间" label-width="120px" prop="pileUpSpace">
          <el-input v-model="material.pileUpSpace" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" label-width="120px" prop="price">
          <el-input v-model="material.stock" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item label="进价" label-width="120px" prop="buyOrSale">
          <el-input v-model="materialprice.inPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价" label-width="120px" prop="buyOrSale">
          <el-input v-model="materialprice.outPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="供应商" label-width="120px" prop="supplier">
          <el-input v-model="materialprice.supplier" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button type="primary" @click="save('material')">{{ btnText }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Material",
  data() {
    return {
      tableData: [],
      currentPage: 1,
      name: "",
      username: "",
      gender: "",
      pageSize: 5,
      total: 0,
      dialogFormVisible: false,
      btnText: "",
      material: {},
      materialprice:{},
      multipleSelection: [],
      rules: {
        // password: [
        //   {required: true, message: '请填写该字段', trigger: 'blur'},
        // ],
        // phoneNumber: [
        //   {required: true, message: '请输入电话号码', trigger: 'blur'},
        //   {min: 11, max: 11, message: '请输入长度为11个数字的电话号码', trigger: 'blur'}
        // ],
      },
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("/materialInfo/get",
          {params: {pageNum: this.currentPage, pageSize: this.pageSize}}
      ).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.currentPage = res.data.current
        this.pageSize = res.data.size
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },
    handleAdd() {
      this.btnText = "添加"
      this.material = {}
      this.materialprice = {}
      this.dialogFormVisible = true
    },
    handleEdit(row) {
      this.btnText = "修改"
      this.dialogFormVisible = true
      this.material = Object.assign({}, row)
      this.materialprice=this.material.materialprice
    },
    save(material) {
      this.$refs[material].validate((valid) => {
        if (valid) {
          this.material.materialprice=this.materialprice
          console.log(this.material)
          this.dialogFormVisible = false
          if (this.btnText==='添加'){

            request.post("materialInfo/add", this.material).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }else{
            request.put("materialInfo/update", this.material).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    del(id) {
      request.delete("materialInfo/delmore/" + id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
      console.log(id)
    },
    handleSelectionChange(val) {
      // console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      //map函数：数组的每个元素都执行一个给定的函数，返回值仍为一个数组
      let ids = this.multipleSelection.map(v => v.id)
      console.log(ids)
      if (ids.length !== 0) {
        request.delete("materialInfo/delmore/"+ids.toString(), ).then(res => {
          if (res) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      } else {
        this.$message.warning("请选择要删除的数据")
      }
    },
    search() {
      if (this.gender === " ") {
        this.gender = ""
      }
      request.get("/material/selectbycondition", {
        params: {
          name: this.name,
          username: this.username,
          gender: this.gender,
        }
      }).then(res => {
        this.tableData = res
        this.total = this.tableData.length
        this.currentPage = 0
      })
    },
    reset() {
      this.username = ""
      this.name = ""
      this.gender = ""
      this.load()
    },
  }
}
</script>

<style scoped>

</style>